package com.itheima.prize.commons.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.prize.commons.db.entity.CardUser;
import com.itheima.prize.commons.db.entity.ViewCardUserHit;
import com.itheima.prize.commons.db.service.ViewCardUserHitService;
import com.itheima.prize.commons.db.mapper.ViewCardUserHitMapper;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PageBean;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
* @author shawn
* @description 针对表【view_card_user_hit】的数据库操作Service实现
* @createDate 2023-12-26 11:58:48
*/
@Service
public class ViewCardUserHitServiceImpl extends ServiceImpl<ViewCardUserHitMapper, ViewCardUserHit>
    implements ViewCardUserHitService{
    /**
     * 用户信息
     * @param
     */
    @Override
    public ApiResult userInfo(Integer id) {
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",id);
        return new ApiResult(1,"个人信息查询成功",queryWrapper);
    }

    /**
     * 我的奖品
     * @param gameid
     * @param curpage
     * @param limit
     * @return
     */
    @Override
    public ApiResult productsInfo(int gameid, int curpage, int limit,HttpServletRequest request) {
        //设置分页信息
        CardUser user = (CardUser) request.getSession().getAttribute("user");
        Integer id = user.getId();
        QueryWrapper<ViewCardUserHit> viewCardUserHitQueryWrapper = new QueryWrapper<ViewCardUserHit>()
                .eq("userid", id);
        Page<ViewCardUserHit> viewCardUserHitPage = baseMapper.selectPage(new Page<>(curpage, limit), viewCardUserHitQueryWrapper);
        Page<ViewCardUserHit> cardUserHitPage = viewCardUserHitPage.setRecords(baseMapper.selectList(viewCardUserHitQueryWrapper));
        if (viewCardUserHitPage.getRecords() == null) {
            return new ApiResult(-1,"失败",null);
        }
        return  new ApiResult(1,"成功",new PageBean<>(cardUserHitPage));
    }

    /**
     * 获取中奖列表
     * @param gameid
     * @param curpage
     * @param limit
     * @return
     */
    @Override
    public ApiResult<PageBean<ViewCardUserHit>> getListInfo(int gameid, int curpage, int limit) {
        QueryWrapper<ViewCardUserHit>productQueryWrapper = new QueryWrapper<ViewCardUserHit>()
                .eq("gameid",gameid);
        List<ViewCardUserHit> viewCardUserHits = baseMapper.selectList(productQueryWrapper);
        Page<ViewCardUserHit> viewCardUserHitPage = baseMapper.selectPage(new Page<>(curpage, limit), productQueryWrapper);
        Page<ViewCardUserHit> cardUserHitPage = viewCardUserHitPage.setRecords(viewCardUserHits);
        if (viewCardUserHitPage.getRecords() == null || viewCardUserHitPage.getRecords().isEmpty()){
            return new ApiResult(-1,"失败",null);
        }
        return new ApiResult(1,"成功",new PageBean<>(cardUserHitPage));
    }
}




